package cn.leetcode;

import java.util.ArrayList;
import java.util.List;

public class Code114 {
    public void flatten(TreeNode root) {
        List<TreeNode> list = new ArrayList<>();
        fun(root, list);
        for (int l = 0; l < list.size() - 1; l++) {
            TreeNode treeNode1 = list.get(l);
            int ll = l + 1;
            TreeNode treeNode2 = list.get(ll);
            treeNode1.left = null;
            treeNode1.right = treeNode2;
            treeNode2.left = null;
            treeNode2.right = null;
        }
    }

    public void fun(TreeNode root, List<TreeNode> list) {
        if(root == null){
            return;
        }
        list.add(root);
        fun(root.left, list);
        fun(root.right, list);
    }
}
